<?php

namespace App\Service;


use App\Model\BorrowModel;
use App\Model\UserModel;

class UserService
{
    private $userModel;
    private $borrowModel;

    /**
     * UserService constructor.
     */
    public function __construct()
    {
        $this->userModel = new UserModel();
        $this->borrowModel = new BorrowModel();
    }

    /**
     * @param $username
     * @param $password
     * 用户登录检测
     */
    public function loginUser($username, $password, $type)
    {
        $UserInfo = $this->userModel->findUserByUsername($username);
//        dump($UserInfo);die;
        if ($type == $UserInfo[0]['usertype']) {
            if ($password == $UserInfo[0]['password']) {
                //验证真确将结果写到session中
                session([
                    'user'=> array(
                        'userid' => $UserInfo[0]['userid'],
                        'username' => $UserInfo[0]['username'],
                        'realname' => $UserInfo[0]['realname'],
                    )
                ]);
                return $this->getResult(1, '登录成功');
            }
            return $this->getResult(2, "密码错误");
        }
        return $this->getResult(3, "用户不存在或用户类型错误");
    }

    /**
     * @return mixed
     * 获取用户列表
     */
    public function getUser($username = "")
    {
        $username = '%'.$username.'%';
        return $this->userModel->getUser($username)->toArray();
    }

    public function addUser($username, array $UserInfo)
    {
        if($this->userModel->findUserByUsername($username)->toArray() == null) {
            if($this->userModel->findUserById($UserInfo['userid'])->toArray() == null) {
                if ($this->userModel->addUser($UserInfo)) {
                    return $this->getResult(1, '注册成功');
                }
            }
            return $this->getResult(0,'这个学号已经被注册过');
        }
        return $this->getResult(0, '此用户名已经被注册');
    }

    /**
     * @param $userid
     * @return string
     */
    public function deleteUserById($userid)
    {
        if($this->userModel->deleteUserById($userid)) {
            return $this->getResult(1,'删除成功');
        }
        return $this->getResult(0,'删除失败');
    }

    /**
     * @param $userid
     * @return array
     */
    public function getBorrow($userid)
    {
        return array_values($this->borrowModel->getBookBorrowByUser($userid)->toArray());
    }

    //解锁用户
    public function unlockUser($userid)
    {
        if ($this->userModel->editStatus($userid)) {
            return $this->getResult(1,'解锁成功');
        }
        return $this->getResult(0,'解锁失败');
    }

    /**
     * @param int $status
     * @param null $data
     * @param null $mag
     * @return string
     */
    private function getResult($status = 0, $mag = null, $data = null)
    {
        return json_encode(array(
            'status' => $status,
            'mag'   => $mag,
            'data'  => $data,
        ));
    }
}